package com.banshuibao.bsb_android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.fragment.O2OFragment;
import com.banshuibao.bsb_android.fragment.SendQuestionFragment;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.IsAdoptModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;

public class ConsultingOfficeActivity extends BaseActivity {

    private O2OFragment o2OFragment;//专家列表

    private RadioGroup radioGroup;

    private FragmentManager manager;

    private SendQuestionFragment sendQuestionFragment;

    private TextView title_name,menu;

    private ImageView search;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consulting_office);
        initView();
    }

    public void initView() {
        radioGroup = findViewById(R.id.rg);
        title_name = findViewById(R.id.title_name);
        menu = findViewById(R.id.menu);
        search = findViewById(R.id.search);
        manager = getSupportFragmentManager();
        getUserRole();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                hideFragment(o2OFragment, sendQuestionFragment);
                FragmentTransaction transaction = manager.beginTransaction();
                switch (i) {
                    case R.id.title1:
                        menu.setVisibility(View.GONE);
                        search.setVisibility(View.VISIBLE);
                        if (o2OFragment == null) {
                            o2OFragment = new O2OFragment();
                            transaction.add(R.id.frameLayout, o2OFragment);
                        } else
                            transaction.show(o2OFragment);
                        transaction.commit();
                        break;
                    case R.id.title2:
                        isAdopt(transaction);
                        break;
                }
            }
        });
    }

    public  void showTitle(FragmentTransaction transaction){
        menu.setVisibility(View.VISIBLE);
        search.setVisibility(View.GONE);
        if (sendQuestionFragment == null) {
            sendQuestionFragment = new SendQuestionFragment();
            transaction.add(R.id.frameLayout, sendQuestionFragment);
        } else
            transaction.show(sendQuestionFragment);
        transaction.commit();
    }

    private void hideFragment(Fragment... fragments) {
        for (Fragment fragment : fragments) {
            if (fragment != null)
                manager.beginTransaction().hide(fragment).commit();
        }
    }

    public void setDefault(){
        radioGroup.check(R.id.title1);
        menu.setVisibility(View.GONE);
        search.setVisibility(View.VISIBLE);
        FragmentTransaction transaction = manager.beginTransaction();
        if (o2OFragment == null) {
            o2OFragment = new O2OFragment();
            transaction.add(R.id.frameLayout, o2OFragment);
        } else
            transaction.show(o2OFragment);
        transaction.commit();

    }
    public void getUserRole(){
        GetLoginInfo getLoginInfo = GetLoginInfo.getLoginInfoInstance(this).readUser();
        String rapidLimit = getLoginInfo.getRapidLimit();
        if(getLoginInfo.getRole().equals("0")){
            if(rapidLimit.equals("0")){
               setDefault();
            }else{
                radioGroup.setVisibility(View.GONE);
                title_name.setVisibility(View.VISIBLE);
                title_name.setText("一对一咨询");
                setDefault();
            }
        }else{
            setDefault();
        }
    }

    //查询是否有未采纳的问题
    private  void isAdopt(final FragmentTransaction transaction) {
        showProgressDialog();
        GetLoginInfo loginInfoInstance = GetLoginInfo.getLoginInfoInstance(context).readUser();
        HttpUtil.doPost(Constants.Url.IS_ADOPT, null, new HttpResponse(context, IsAdoptModel.class) {
            @Override
            public void onResult(Object obj) {
                IsAdoptModel s = (IsAdoptModel) obj;
                boolean data = s.isData();
                if (data) {
                    Intent intent = new Intent(context, TabActivity.class);
                    intent.putExtra("flag", 1);
                    startActivity(intent);
                    finish();
                }else{
                    showTitle(transaction);
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }
        });
    }
}
